<template>
	<view>
		<view v-if="!open" id="_drag_button"  :style="'left: ' + left + 'px; top:' + top + 'px;'"
			@touchstart="touchstart" @touchmove.stop.prevent="touchmove" @touchend="touchend"
			@click.stop.prevent="click" :class="[{transition: isDock && !isMove  },showVisible?'drag':'']">
			<text class="iconfont icon-gongjubao blue imgicon"></text>
		</view>
		<view class="markleft" v-if="open">
			<view :class="[toppading?'mark-cont1':'mark-cont']">
				<view class="title">日志</view>
				<view class="mark-cont-row" v-for="(item,index) in logs">
					<view>{{item.time}}  {{item.msg}}</view>
					 <view>
						 请求参数:{{item.reqdata}}
					 </view>
					 <view>
						 响应内容:{{item.respdata}}
					 </view>
				</view>
				 
			</view>
		</view>
		<view class="markmask" @click="maskClick" v-if="open">

		</view>
	</view>
</template>

<script> 
	export default {
		name: 'drag-button',
		props: {
			isDock: {
				type: Boolean,
				default: false
			},
			existTabBar: {
				type: Boolean,
				default: false
			},
			times: {
				type: Number,
				value: 0
			},
			toppading: {
				type: Boolean,
				default: false
			},
			
			open:{
				type: Boolean,
				default: false
			},
			showVisible:{
				type:Boolean,
				default:false
			}
		},
		watch: { 
		},
		data() {
			return {
				//open: false,
				top: 0,
				left: 0,
				width: 0,
				height: 0,
				offsetWidth: 0,
				offsetHeight: 0,
				windowWidth: 0,
				windowHeight: 0,
				isMove: true,
				edge: 10,
				
				logs:[],
				 
			}
		},
		mounted() {
			var self = this;
			const sys = uni.getSystemInfoSync();
			this.windowWidth = sys.windowWidth;
			this.windowHeight = sys.windowHeight;
			// #ifdef APP-PLUS
			this.existTabBar && (this.windowHeight -= 50);
			// #endif
			if (sys.windowTop) {
				this.windowHeight += sys.windowTop;
			}
			//console.log(sys)

			const query = uni.createSelectorQuery().in(this);
			query.select('#_drag_button').boundingClientRect(data => {
				self.width = data.width;
				self.height = data.height;
				self.offsetWidth = data.width / 2;
				self.offsetHeight = data.height / 2;
				self.left = self.windowWidth - self.width - self.edge;
				self.top = self.windowHeight - self.height - self.edge;
				self.top = self.top * 0.4;
			}).exec();
			self.logs = self.$lobster.logs.logs;
			
			uni.$on("logs",function(data){
				self.open=true;
			})
		},
		methods: {
			 
			maskClick() {
				this.open = false;
			},
			click() {
				this.open = true;
				this.$emit('btnClick');
			},
			touchstart(e) {
				// this.$emit('btnTouchstart');
			},
			touchmove(e) {
				// 单指触摸
				if (e.touches.length !== 1) {
					return false;
				}
				this.isMove = true;
				this.left = e.touches[0].clientX - this.offsetWidth;

				let clientY = e.touches[0].clientY - this.offsetHeight;
				// #ifdef H5
				clientY += this.height;
				// #endif
				let edgeBottom = this.windowHeight - this.height - this.edge;

				// 上下触及边界
				if (clientY < this.edge) {
					this.top = this.edge;
				} else if (clientY > edgeBottom) {
					this.top = edgeBottom;
				} else {
					this.top = clientY
				}

			},
			touchend(e) {
				if (this.isDock) {
					let edgeRigth = this.windowWidth - this.width - this.edge;
					if (this.left < this.windowWidth / 2 - this.offsetWidth) {
						this.left = this.edge;
					} else {
						this.left = edgeRigth;
					}
				}
				this.isMove = false; 
			}, 
		}
	}
</script>

<style lang="scss" scoped>
	.title{
		text-align: center;
		background-color: #EEEEEE;
		line-height: 60rpx;
		position: fixed;
		top:50%;
		left: 0;
		right: 0;
	}
	.row-allergies {
		line-height: 40rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.page-row {
		display: flex;
		justify-content: space-between;
		align-items: center;
		flex-wrap: nowrap;
	}

	.row-txt {
		padding-left: 20rpx;
	}

	.lineboder {
		padding-top: 10rpx;
		border-bottom: 1px solid #eeeeee;
	}

	.mark-cont-allergies {
		padding: 10rpx 0;
		font-size: 28rpx;
		color: #666666;
	}

	.mark-cont-row {
		font-size: 28rpx;
		color: #666666;
		line-height: 60rpx;
		word-break: break-word;
		//margin-bottom: 20rpx;
		border-bottom:  2rpx solid #eee;
	}

	.mark-cont {
		padding: 60rpx 30rpx 40rpx 30rpx;
	}

	.mark-cont1 {
		padding: 60rpx 20rpx 20rpx 20rpx;
	}

	.mark-cont-title {
		font-size: 32rpx;
		color: #333333;
		font-weight: 500;
	}

	.mark-cont-title::before {
		content: " ";
		display: inline-block;
		height: 32rpx;
		border-left: 8rpx solid #4990e2;
		vertical-align: middle;
		margin-right: 10rpx;
		margin-bottom: 6rpx;
	}

	.markleft {
		overflow-y: auto;
		position: fixed;
		background-color: #FFFFFF;
		bottom:0 ;
		left: 0;
		width: 100%;
		height: 50%;
		z-index: 999;
	}

	.markmask {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		height: 100%;
		background-color: #000000;
		opacity: 0.5;
		z-index: 998;
	}

	.imgicon {
		font-size: 40rpx !important;
	}

	.drag {
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: #FFFFFF;
		box-shadow: 0 0 6rpx #4990e2;
		color: $uni-text-color-inverse;
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
		font-size: $uni-font-size-sm;
		position: fixed;
		z-index: 998;
		&.transition {
			transition: left .3s ease, top .3s ease;
		}
	}
</style>
